﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using GeekShop.Commands;

namespace GeekShop.Tools.Transform
{
    public class Move : MouseBehaviour, ITool
    {
        #region Fields
        private ICommand command;
        #endregion

        #region Properties
        public Cursor Cursor
        {
            get
            {
                return Cursors.SizeAll;
            }
        }
        public Bitmap Icon
        {
            get
            {
                return GeekShop.Properties.Resources.transform_move;
            }
        }
        public string Name
        {
            get { return "Move"; }
        }
        public ICommand Command { get { return this.command; } }
        #endregion

        #region Methods
        public void RunDialog(PictureManager pictureManager)
        {
            base.picture = pictureManager.SelectedPicture;
        }
        #endregion

        #region Events
        public event EventHandler ToolReady;
        #endregion

        #region Event Methods
        public override void MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //todo: cancel dragging if escape is pressed
                Point location = base.picture.SelectedLayer.Location;
                location.X += e.Location.X - base.start.X;
                location.Y += e.Location.Y - base.start.Y;

                base.buffer = (Bitmap)base.preview.Clone();
                Graphics g = Graphics.FromImage(this.buffer);
                g.DrawImage(base.picture.SelectedLayer.Bitmap, location.X, location.Y, base.picture.SelectedLayer.Bitmap.Width, base.picture.SelectedLayer.Bitmap.Height);
                base.graphics.DrawImage(base.buffer, 0, 0);
            }
        }
        public override void MouseUp(object sender, MouseEventArgs e)
        {
            Size size = new Size(e.Location.X - this.start.X, e.Location.Y - this.start.Y);
            this.command = new Commands.Transform.Move(this.picture.SelectedLayer, size);
            this.ToolReady(this, new EventArgs());
        }
        #endregion
    }
}
